package forum;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;

public class SubForum implements Serializable{
	private int subForumID;
	private String name;
 	private int numberOfThreads;
 	private Timestamp createdOn;
	private int minimumlvl;
	private Thread latestThread;
	private forum.Thread[] threads;
	
	public SubForum(){
		
	}
 	public SubForum(int subForumID, String name, int numberOfThreads, Timestamp createdOn, int minimumRestrictionlvl) {
		this.subForumID = subForumID;
		this.name = name;
		this.numberOfThreads = numberOfThreads;
		this.createdOn = createdOn;
		this.minimumlvl = minimumRestrictionlvl;
			
	}
 	
 	public SubForum(int subForumID, String name, int minimumRestrictionlvl) {
		this.subForumID = subForumID;
		this.name = name;
		this.numberOfThreads = 0;
		this.createdOn = new Timestamp(System.currentTimeMillis());
		this.minimumlvl = minimumRestrictionlvl;

	}
 	
 	public synchronized void Create() {
 		Connection conn = db_code.connect();
 		try {
			
			Statement stmt = conn.createStatement();
			Timestamp createdOnTime = new Timestamp(System.currentTimeMillis());
			
			String query = "INSERT INTO subforum (name, numberOfThreads, createdOn, minimumRestrictionlvl)" +
					"VALUES('" + 
						name + "'," + 
						numberOfThreads + ",'" + 
						createdOnTime + "'," + 
						minimumlvl + ");";
			
			stmt.executeUpdate(query,Statement.RETURN_GENERATED_KEYS);
			
			ResultSet rs = stmt.getGeneratedKeys();
			if ( rs.next() ) {
			    int key = rs.getInt(1);
			    setSubForumID(key);
			}
			db_code.disconnect(conn);
			
		} catch (Exception e) {
			db_code.disconnect(conn);
			System.out.println("FAIL! INSERT!");
			e.printStackTrace();
		}
 		
	}
 	
 	public synchronized void Edit(int subForumID, String name, int numberOfThreads, Date createdOn, int minimumRestrictionlvl) {
 		
 		Connection conn = db_code.connect();
 		try {
 			
			Statement stmt = conn.createStatement();
			String query = "UPDATE subforum " +
					"SET " +
						"name = '" + name + "'," +
						"numberOfThreads = " + numberOfThreads + "," +
						"createdOn = '" + createdOn +"',"+
						"minimumRestrictionlvl = " + minimumRestrictionlvl +
					"WHERE " +
						"subForumID = " + subForumID;
			
			stmt.execute(query);
			
		} catch (Exception e) {
			db_code.disconnect(conn);
			System.out.println("FAIL! UPDATE!");
			e.printStackTrace();
		}
 		
	}
 	
 	public synchronized void Delete(){
 		Connection conn = db_code.connect();
 		try {
			Statement stmt = conn.createStatement();
			String query = "DELETE FROM subforum WHERE subForumID = " + subForumID;
			
			stmt.execute(query);
		} catch (Exception e) {
			db_code.disconnect(conn);
			System.out.println("FAIL! DELETE!");
			e.printStackTrace();
		}
 		
 	}
 	
 	public synchronized void incrementNumberOfThreads(){
 		
 		Connection conn = db_code.connect();
 		try{
 			
			Statement stmt = conn.createStatement();
			String query = "UPDATE subforum SET numberOfThreads=numberOfThreads+1 WHERE subForumID = " + subForumID;
			stmt.execute(query);
			
			numberOfThreads++;
			db_code.disconnect(conn);
		}
		catch(SQLException e){
			db_code.disconnect(conn);
			System.out.println("SQL statement error!");
			e.printStackTrace();
		}
 		
 	}


 	public ArrayList<Thread> getAllThreads(){
		
		ArrayList<Thread> result = new ArrayList<Thread>();
		Connection conn = db_code.connect();
		try{
			
			Statement stmt = conn.createStatement();
			ResultSet rs = null;
			
			String query = "SELECT * FROM thread WHERE subForumID = " + subForumID;
			
			rs = stmt.executeQuery(query);
			
			Thread row;
			while(rs.next()){
				row  = new Thread(
						(Integer)rs.getObject("threadID"), 
						(Integer)rs.getObject("subForumID"), 
						(String)rs.getObject("name"), 
						(Integer)rs.getObject("createdBy"), 
						(Timestamp)rs.getObject("createdOn"), 
						(Integer)rs.getObject("views"), 
						(Integer)rs.getObject("replies"));
				result.add(row);
			}
			db_code.disconnect(conn);
			return result;
		}
		catch(SQLException e){
			System.out.println("SQL statement error!");
			e.printStackTrace();
		}
		db_code.disconnect(conn);
		return null;
 	}
 	
	public int getSubForumID() {
		return subForumID;
	}


	public void setSubForumID(int subForumID) {
		this.subForumID = subForumID;
	}


	public String getName() {
		if(this.name == null || this.name == ""){
			Connection conn = db_code.connect();
			try{
				
				Statement stmt = conn.createStatement();
				ResultSet rs = null;
				
				String query = "SELECT name FROM subforum WHERE subForumID = " + subForumID +" LIMIT 1;";
				
				rs = stmt.executeQuery(query);
				
				rs.next();
				this.name = rs.getString("name");
				db_code.disconnect(conn);
			}
			catch(SQLException e){
				System.out.println("SQL statement error!");
				e.printStackTrace();
				db_code.disconnect(conn);
			}
		}
		return name;
	}


	public void setName(String name) {
		this.name = name;
	}


	public int getNumberOfThreads() {
		return numberOfThreads;
	}


	public void setNumberOfThreads(int numberOfThreads) {
		this.numberOfThreads = numberOfThreads;
	}


	public Date getCreatedOn() {
		return createdOn;
	}


	public void setCreatedOn(Timestamp createdOn) {
		this.createdOn = createdOn;
	}


	public int getMinimumlvl() {
		return minimumlvl;
	}


	public void setMinimumlvl(int minimumlvl) {
		this.minimumlvl = minimumlvl;
	}
	
	public Thread getLatestThread() {
		Connection conn = db_code.connect();
		try{
			
			Statement stmt = conn.createStatement();
			ResultSet rs = null;
			
			String query = "SELECT * FROM thread WHERE subForumID = " + this.subForumID +" ORDER BY createdOn DESC LIMIT 1;";
			
			rs = stmt.executeQuery(query);
			while(rs.next()){
				this.latestThread  = new Thread(
						(Integer)rs.getObject("threadID"), 
						(Integer)rs.getObject("subForumID"), 
						(String)rs.getObject("name"), 
						(Integer)rs.getObject("createdBy"), 
						rs.getTimestamp("createdOn"), 
						(Integer)rs.getObject("views"), 
						(Integer)rs.getObject("replies"));;
			}
			db_code.disconnect(conn);
			return this.latestThread;
		}
		catch(SQLException e){
			System.out.println("SQL statement error!");
			e.printStackTrace();
		}
		db_code.disconnect(conn);
		return null;
	}

	public void setLatestThread(Thread latestThread) {
		this.latestThread = latestThread;
	}

	public forum.Thread[] getThreads() {
		ArrayList<forum.Thread> result = new ArrayList<forum.Thread>();
		Connection conn = db_code.connect();
		try{
			Statement stmt =conn.createStatement();
			ResultSet rs = null;
			
			String query = "SELECT * FROM thread WHERE subForumID = " + subForumID +" ORDER BY createdOn DESC;";
			
			rs = stmt.executeQuery(query);
			Thread row;
			int i= 0;
			while(rs.next()){
				row  = new Thread(
						(Integer)rs.getObject("threadID"), 
						(Integer)rs.getObject("subForumID"), 
						(String)rs.getObject("name"), 
						(Integer)rs.getObject("createdBy"), 
						(Timestamp)rs.getObject("createdOn"), 
						(Integer)rs.getObject("views"), 
						(Integer)rs.getObject("replies"));
				result.add(row);
				i++;
			}
			threads = new forum.Thread[i];
			for(int index =0;index<i;index++){
				threads[index] = result.get(index);
			}
			db_code.disconnect(conn);
			return threads;
		}
		catch(SQLException e){
			System.out.println("SQL statement error!");
			e.printStackTrace();
		}
		db_code.disconnect(conn);
		return null;
	}

	public void setThreads(forum.Thread[] threads) {
		this.threads = threads;
	}
}
